﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;

public class CustomBasePage : System.Web.UI.Page
{
  private string pageName;
  private int guid;
  #region Properties




  public string DomainName
  {
    get
    {
      if (Session["global.DomainName"] == null)
        Session["global.DomainName"] = String.Empty;

      return System.Convert.ToString(Session["global.DomainName"]);
    } //eof get
    set { Session["global.DomainName"] = value; } //eof set
  } //eof property


  public int GUID
  {
    get
    {
      return this.guid;
    } //eof get
    set { this.guid = value; } //eof set
  } //eof property GUID





  public string PageName
  {
    get
    {
      return System.IO.Path.GetFileNameWithoutExtension(
                    System.Web.HttpContext.Current.Request.Url.AbsolutePath);
    } //eof get
    set { pageName = value; } //eof set
  } //eof property MyPageName

  private string _ConnectionString;
  public string ConnectionString
  {
    get
    {
      if (System.Web.HttpContext.Current.Session["global.connectionStr"] == null)
        return System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
      else
        return System.Convert.ToString(System.Web.HttpContext.Current.Session["global.connectionStr"]);
    } //eof get
    set
    {
      this._ConnectionString = value;
      System.Web.HttpContext.Current.Session["global.connectionStr"] = this._ConnectionString;
    }

  }  //eof property


  #endregion //Properties



  override protected void OnInit(EventArgs e)
  {
    base.OnInit(e);

  } //eof OnInit

  //comm_OnPreRender
  //comm - here we do populate which elements should go rounded. Note: one fails - ALL FAIL!!!!!
  protected override void OnPreRender(EventArgs e)
  {


    
    base.OnPreRender(e); //put this at the end 
  }




  #region UtilityMethods


  /// <summary>
  /// This method relies on the convention practice that the Session keys would be named as follows:
  /// pageName.variableName - if the Session variable is page specific and 
  /// global.variableName - if the Session variable should be trully global
  /// </summary>
  private void ClearAllNonGlobalSessionsButMine()
  {
    string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(
                            System.Web.HttpContext.Current.Request.Url.AbsolutePath);

    for (int i = 0; i < HttpContext.Current.Session.Count; i++)
    {
      string keyValue = HttpContext.Current.Session.Keys[i];
      if (keyValue.Contains(fileNameNoExt) || keyValue.Contains("global"))
      { ; }				//do nothing = preserve the value in the session 
      else
      {
        //answers the question: what session variables are deleted during page change
        /*debugStart
        Utils.Debugger.WriteIf ( " Set to null ---- " + HttpContext.Current.Session.Keys [ i ] + " -- with value -- " + 
            HttpContext.Current.Session [ HttpContext.Current.Session.Keys [ i ] ] );
        /*debugEnd*/
        HttpContext.Current.Session[HttpContext.Current.Session.Keys[i]] = null;
      } //eof else if it is global or belongs to current page
    } //eof loop 

  } //eof public static void ClearAllButCurrent (string pageName )  

  public void ClearAllNonGlobalSessionsAndMine()
  {
    string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(
                            System.Web.HttpContext.Current.Request.Url.AbsolutePath);

    for (int i = 0; i < HttpContext.Current.Session.Count; i++)
    {
      string keyValue = HttpContext.Current.Session.Keys[i];
      if (keyValue.Contains("global"))
      { ; }				//do nothing = preserve the value in the session 
      else
      {
        //answers the question: what session variables are deleted during page change
        /*debugStart
        Utils.Debugger.WriteIf ( " Set to null ---- " + HttpContext.Current.Session.Keys [ i ] + " -- with value -- " + 
            HttpContext.Current.Session [ HttpContext.Current.Session.Keys [ i ] ] );
         /*debugEnd*/
        HttpContext.Current.Session[HttpContext.Current.Session.Keys[i]] = null;
      } //eof else if it is global or belongs to current page
    } //eof loop 

  } //eof public static void ClearAllButCurrent (string pageName )  

 

  #endregion UtilityMethods

  #region DataMethods
  public DataSet PopulateListOfDbObjects(ref string msg , string typeOfObject )
  {
    Utils.Debugger.WriteIf("START METHOD --- PopulateListOfDbObjects ");

    #region Instantiation
    string connectionStr = Convert.ToString(Session["global.connectionStr"]);
    string domainName = this.DomainName;
    Utils.Debugger.WriteIf("CHECK --- PopulateListOfDbObjects connectionStr is " + connectionStr);
    

    DbControllers.TableControl tco = new DbControllers.TableControl(connectionStr);
    DataSet dsForTables = new DataSet();

    #endregion //Instantiation

    //skok_PopulateListOfDbObjects
    if (tco.ListMetaDataColumn(ref msg, ref typeOfObject, ref dsForTables))
    {
      //debugOk Utils.Debugger.DebugDataSet ( "from PopulateListOfDbObjects ", ref dsForTables );
      //variant Session [base.PageName + ".dsForTables"] = dsForTables;
      return dsForTables;
    }
    else
    {
      return null;
    } //eof method else if the retrieving of the MetaDatacolumn is ok 

  } //eof method private void PopulateListOfDbObjects

  #endregion DataMethods

} //eof BasePageClass

